// pages/xdzglogin/xdzglogin.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: ''
  },
  tozx() {
    wx.navigateTo({
      url: '../zx/zx',
    })
  },
  copy() {
    const _ = this;
    const ma = _.data.lists[0].ma
    wx.setClipboardData({
      data: ma,
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: '内容已复制！',
              duration: 5000
            })
          }
        })
      }
    })
  },
  getPhoneNumber(e) {
    var _ = this;
    var area = _.data.area;
    var agent = _.data.agent;
    var flag = _.data.flag;
    if (flag) {
      wx.cloud.callFunction({
        name: 'getMobile',
        data: {
          weRunData: wx.cloud.CloudID(e.detail.cloudID),
        }
      }).then(res => {
        wx.setStorageSync('lwccphone', res.result)
        var phone = res.result
        _.setData({
          mobile: res.result,
        })
        wx.request({
          url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
          method: 'POST',
          data: {
            activeId: 25,
            mobile: res.result,
            areaCode: _.data.area,
            agent: _.data.agent,
            pageId: _.data.page
          },
          success: res => {

          }
        })
        wx.request({ //发送请求
          url: "https://zg99.offcn.com/index/chaxun/register/?actid=50013&callback=?",
          data: {
            phone: phone,
            isagree: true,
            area: area,
            agent: agent
          }, //接收参数
          success(res) { //返回
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            };
            console.log(theJson)
            wx.request({ //发送请求
              url: "https://zg99.offcn.com/index/chaxun/getphonestatus/?actid=50013&callback=?",
              data: {
                phone: phone,
                limits: 600
              }, //接收参数
              success(res) { //返回
                var jsonStr = res.data;
                jsonStr = jsonStr.replace("(", "");
                jsonStr = jsonStr.replace(")", "");
                if (typeof jsonStr != 'object') {
                  jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                  var theJson = JSON.parse(jsonStr);
                };
                console.log(theJson.user.id)
                const num = theJson.user.id
                if (num > 590) {
                  _.setData({
                    dy: 1,
                    lists: 1
                  })
                } else {
                  wx.request({ //发送请求
                    url: "https://zg99.offcn.com/index/chaxun/getfzinfo/?actid=50013&callback=?",
                    data: {
                      hao: num,
                      limits: 600
                    }, //接收参数
                    success(res) { //返回
                      var jsonStr = res.data;
                      jsonStr = jsonStr.replace("(", "");
                      jsonStr = jsonStr.replace(")", "");
                      if (typeof jsonStr != 'object') {
                        jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                        var theJson = JSON.parse(jsonStr);
                      };
                      console.log(theJson.lists)
                      _.setData({
                        lists: theJson.lists
                      })
                    }
                  })
                }

              }
            })
          }
        })
      }).catch(err => {
        console.error(err);
      });
    } else {
      wx.showToast({
        title: '请先勾选隐私协议！',
        icon: 'none'
      })
    }

  },
  xz: function (e) {
    var _ = this;
    var flag = e.detail.value[0];
    if (flag == undefined) {
      flag = false
    } else {
      flag = true
    }
    _.setData({
      flag
    })
    console.log(_.data.flag)
  },
  xy: function (e) {
    var _ = this;
    wx.navigateTo({
      url: '../agreement/agreement',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _ = this;
    console.log(options)
    _.setData({
      area: options.area,
      agent: options.agent,
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }

    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/gettotal/?actid=50013&callback=?",
      data: {
        phone: 15205451183,
        sstimes: Date.parse(new Date())
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson)
        const left = 590 - theJson.zcounts

        if (left > 0) {
          _.setData({
            left: left
          })
        } else {
          _.setData({
            left: 0
          })
        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享 
   */
  onShareAppMessage() {
    var _ = this;
    return {
      title: '论文查重券领取',
      path: 'pages/lwcc/lwcc?area=' + _.data.area + '&agent=' + _.data.agent + '&page=' + _.data.page
    }
  }
})